package com.wetao.testrxjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jakewharton.rxbinding3.view.RxView;
import com.jakewharton.rxbinding3.widget.RxAdapterView;
import com.jakewharton.rxbinding3.widget.RxTextView;
import com.jakewharton.rxbinding3.widget.TextViewTextChangeEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import kotlin.Unit;

public class TestRxBindingActivity extends AppCompatActivity {
    private static final String TAG = "TestRxBindingActivity";
    private List<String> datas  = new ArrayList<>();
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_rx_binding);
        Button btn_rx_click = findViewById(R.id.btn_rx_click);
        //使用throttleFirst()来实现防抖动（例如我们设置为2秒，那么用户不论在2秒的时间内点击多少次，我们只响应一次点击事件）
        RxView.clicks(btn_rx_click)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Unit>() {
                    @Override
                    public void accept(Unit unit) throws Exception {
                        Log.i(TAG, "单击：" + unit);
                    }
                });
        RxView.longClicks(btn_rx_click)
                .subscribe(new Consumer<Unit>() {
                    @Override
                    public void accept(Unit unit) throws Exception {
                        Log.i(TAG, "长按：" + unit);
                    }
                });
        /*boolean isVisibility = false;
        try {
            RxView.visibility(btn_rx_click).accept(isVisibility);
        } catch (Exception e) {
            e.printStackTrace();
        }*/


        EditText et_rx_change = findViewById(R.id.et_rx_change);
        RxTextView.textChanges(et_rx_change)
                .subscribe(new Consumer<CharSequence>() {
                    @Override
                    public void accept(CharSequence charSequence) throws Exception {
                        Log.i(TAG, "输入1：" + charSequence);
                    }
                });

        EditText et_rx_changeEvents = findViewById(R.id.et_rx_changeEvents);
        RxTextView.textChangeEvents(et_rx_changeEvents)
                .subscribe(new Consumer<TextViewTextChangeEvent>() {
                    @Override
                    public void accept(TextViewTextChangeEvent textViewTextChangeEvent) throws Exception {
                        Log.i(TAG, "输入2：getStart():" + textViewTextChangeEvent.getStart() + ", getBefore()=" + textViewTextChangeEvent.getBefore() + ", getCount()=" + textViewTextChangeEvent.getCount() + ", getText()=" + textViewTextChangeEvent.getText());
                    }
                });


        datas.add("11");
        datas.add("22");
        datas.add("33");
        datas.add("44");
        datas.add("55");

        recyclerView = findViewById(R.id.rv_rx_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, datas);
        recyclerView.setAdapter(adapter);


        /*RxAdapterView.itemClicks(recyclerView)
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {

                    }
                });*/

    }




}
